package com.example.nightmeel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.nightmeel.common.CategoryInfo;
import com.example.nightmeel.db.TimeLineData;
import com.example.nightmeel.db.TimeLineDbDao;
import com.example.nightmeel.db.TimeLineDbHelper;
import com.example.nightmeel.view.CameraView;
import com.example.nightmeel.view.CameraView.SaveImageListener;

public class ItemPostActivity extends Activity implements SaveImageListener, OnClickListener {

    ShutterClickListener mShutterClickListener;
    String mMessage = null;
    Uri mImageUri = null;
    int mSeason = 0;
    int mTimezone = 0;
    int mCategory = 0;
    
    Context context;
    EditText editView;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_item_post);

        context = this;

        mShutterClickListener = (CameraView)findViewById(R.id.post_camera_view);
        Button shutter = (Button)findViewById(R.id.post_shutter_btn);
        shutter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mImageUri == null) {

                    if (mShutterClickListener != null) {
                        mShutterClickListener.onShutterClick();
                    }
                } else {
                    mImageUri = null;
                    CameraView camera = (CameraView) findViewById(R.id.post_camera_view);
                    ImageView image = (ImageView) findViewById(R.id.post_image_view);
                    camera.setVisibility(View.VISIBLE);
                    image.setVisibility(View.GONE);
                    image.setImageURI(null);
                }
            }
        });

        Button message = (Button)findViewById(R.id.post_message_btn);
        message.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
              //テキスト入力を受け付けるビューを作成します。
                editView = new EditText(context);
                editView.setMaxLines(1);
                InputFilter[] _inputFilter = new InputFilter[1];
                _inputFilter[0] = new InputFilter.LengthFilter(32);
                editView.setFilters(_inputFilter);
                new AlertDialog.Builder(context)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle("メッセージ入力")
                    //setViewにてビューを設定します。
                    .setView(editView)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            //入力した文字をトースト出力する
                            Toast.makeText(context,
                                    editView.getText().toString(),
                                    Toast.LENGTH_LONG).show();
                            mMessage = editView.getText().toString();
                        }
                    })
                    .setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    })
                    .show();
            }
        });

        Button geo = (Button)findViewById(R.id.post_geo_btn);
        geo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        Button post = (Button)findViewById(R.id.post_post_btn);
        post.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO デモ版は直接DBに書き込み
                if (mImageUri != null) {
                    TimeLineData data = new TimeLineData();
                    data.setImage(mImageUri.toString());
                    data.setMessage(mMessage);
                    data.setUser_name("新宿太郎");
                    data.setLastupdate(String.valueOf(System.currentTimeMillis()));
                    data.setUser_icon(mImageUri.toString());
                    TimeLineDbHelper helper = new TimeLineDbHelper(getApplicationContext());
                    SQLiteDatabase db = helper.getWritableDatabase();
                    TimeLineDbDao dbDao = new TimeLineDbDao(db);
                    dbDao.insert(data);
                    db.close();
                    finish();
                }
            }
        });
        
        ToggleButton spring = (ToggleButton)findViewById(R.id.post_season_spring_btn);
        spring.setOnClickListener(this);
        ToggleButton summer = (ToggleButton)findViewById(R.id.post_season_summer_btn);
        summer.setOnClickListener(this);
        ToggleButton autumn = (ToggleButton)findViewById(R.id.post_season_autumn_btn);
        autumn.setOnClickListener(this);
        ToggleButton winter = (ToggleButton)findViewById(R.id.post_season_winter_btn);
        winter.setOnClickListener(this);
        
        ToggleButton morning = (ToggleButton)findViewById(R.id.post_timezone_morning_btn);
        morning.setOnClickListener(this);
        ToggleButton day = (ToggleButton)findViewById(R.id.post_timezone_day_btn);
        day.setOnClickListener(this);
        ToggleButton evening = (ToggleButton)findViewById(R.id.post_timezone_evening_btn);
        evening.setOnClickListener(this);
        ToggleButton night = (ToggleButton)findViewById(R.id.post_timezone_night_btn);
        night.setOnClickListener(this);
        
        ToggleButton alcohol = (ToggleButton)findViewById(R.id.post_category_alcohol_btn);
        alcohol.setOnClickListener(this);
        ToggleButton bitter = (ToggleButton)findViewById(R.id.post_category_bitter_btn);
        bitter.setOnClickListener(this);
        ToggleButton bowl = (ToggleButton)findViewById(R.id.post_category_bowl_btn);
        bowl.setOnClickListener(this);
        ToggleButton chinese = (ToggleButton)findViewById(R.id.post_category_chinese_btn);
        chinese.setOnClickListener(this);
        ToggleButton cold = (ToggleButton)findViewById(R.id.post_category_cold_btn);
        cold.setOnClickListener(this);
        ToggleButton drink = (ToggleButton)findViewById(R.id.post_category_drink_btn);
        drink.setOnClickListener(this);
        ToggleButton fish = (ToggleButton)findViewById(R.id.post_category_fish_btn);
        fish.setOnClickListener(this);
        ToggleButton flour = (ToggleButton)findViewById(R.id.post_category_flour_btn);
        flour.setOnClickListener(this);
        ToggleButton hot = (ToggleButton)findViewById(R.id.post_category_hot_btn);
        hot.setOnClickListener(this);
        ToggleButton japanese = (ToggleButton)findViewById(R.id.post_category_japanese_btn);
        japanese.setOnClickListener(this);
        ToggleButton meat = (ToggleButton)findViewById(R.id.post_category_meat_btn);
        meat.setOnClickListener(this);
        ToggleButton noodle = (ToggleButton)findViewById(R.id.post_category_noodle_btn);
        noodle.setOnClickListener(this);
        ToggleButton rice = (ToggleButton)findViewById(R.id.post_category_rice_btn);
        rice.setOnClickListener(this);
        ToggleButton salty = (ToggleButton)findViewById(R.id.post_category_salty_btn);
        salty.setOnClickListener(this);
        ToggleButton sour = (ToggleButton)findViewById(R.id.post_category_sour_btn);
        sour.setOnClickListener(this);
        ToggleButton spicy = (ToggleButton)findViewById(R.id.post_category_spicy_btn);
        spicy.setOnClickListener(this);
        ToggleButton sweet = (ToggleButton)findViewById(R.id.post_category_sweet_btn);
        sweet.setOnClickListener(this);
        ToggleButton vegetable = (ToggleButton)findViewById(R.id.post_category_vegetable_btn);
        vegetable.setOnClickListener(this);
        ToggleButton western = (ToggleButton)findViewById(R.id.post_category_western_btn);
        western.setOnClickListener(this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        RelativeLayout view = (RelativeLayout) findViewById(R.id.post_camera_parent);
        int width = view.getWidth();
        view.setLayoutParams(new LinearLayout.LayoutParams(width, width));
    }

    public interface ShutterClickListener {
        public void onShutterClick();
    }

    @Override
    public void onSaveImage(Uri uri) {
        mImageUri = uri;
        CameraView camera = (CameraView) findViewById(R.id.post_camera_view);
        ImageView image = (ImageView) findViewById(R.id.post_image_view);
        camera.setVisibility(View.GONE);
        image.setVisibility(View.VISIBLE);
        image.setImageURI(uri);
    }

    @Override
    public void onSaveFailedImage() {
        // TODO Auto-generated method stub
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 季節
		case R.id.post_season_spring_btn:
			toggle(TOGGLE_TYPE_SEASON, v.isSelected(), CategoryInfo.SEASON_SPRING);
			break;
		case R.id.post_season_summer_btn:
			toggle(TOGGLE_TYPE_SEASON, v.isSelected(), CategoryInfo.SEASON_SUMMER);
			break;
		case R.id.post_season_autumn_btn:
			toggle(TOGGLE_TYPE_SEASON, v.isSelected(), CategoryInfo.SEASON_AUTUMN);
			break;
		case R.id.post_season_winter_btn:
			toggle(TOGGLE_TYPE_SEASON, v.isSelected(), CategoryInfo.SEASON_WINTER);
			break;
		// 時間帯
		case R.id.post_timezone_morning_btn:
			toggle(TOGGLE_TYPE_TIMEZONE, v.isSelected(), CategoryInfo.TIMEZONE_MORNING);
			break;
		case R.id.post_timezone_day_btn:
			toggle(TOGGLE_TYPE_TIMEZONE, v.isSelected(), CategoryInfo.TIMEZONE_DAY);
			break;
		case R.id.post_timezone_evening_btn:
			toggle(TOGGLE_TYPE_TIMEZONE, v.isSelected(), CategoryInfo.TIMEZONE_EVENING);
			break;
		case R.id.post_timezone_night_btn:
			toggle(TOGGLE_TYPE_TIMEZONE, v.isSelected(), CategoryInfo.TIMEZONE_NIGHT);
			break;
		// カテゴリー
		case R.id.post_category_alcohol_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_ALCOHOL);
			break;
		case R.id.post_category_bitter_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_BITTER);
			break;
		case R.id.post_category_bowl_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_BOWL);
			break;
		case R.id.post_category_chinese_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_CHINESE);
			break;
		case R.id.post_category_cold_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_COLD);
			break;
		case R.id.post_category_drink_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_DRINK);
			break;
		case R.id.post_category_fish_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_FISH);
			break;
		case R.id.post_category_flour_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_FLOUR);
			break;
		case R.id.post_category_hot_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_HOT);
			break;
		case R.id.post_category_japanese_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_JAPANEASE);
			break;
		case R.id.post_category_meat_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_MEAT);
			break;
		case R.id.post_category_noodle_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_NOODLE);
			break;
		case R.id.post_category_rice_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_RICE);
			break;
		case R.id.post_category_salty_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_SALTY);
			break;
		case R.id.post_category_sour_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_SOUR);
			break;
		case R.id.post_category_spicy_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_SPICY);
			break;
		case R.id.post_category_sweet_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_SWEET);
			break;
		case R.id.post_category_vegetable_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_VEGETABLE);
			break;
		case R.id.post_category_western_btn:
			toggle(TOGGLE_TYPE_CATEGORY, v.isSelected(), CategoryInfo.CATEGORY_WESTERN);
			break;
		default:
			break;
		}
		
	}
	
	private static final int TOGGLE_TYPE_SEASON = 0;
	private static final int TOGGLE_TYPE_TIMEZONE = 1;
	private static final int TOGGLE_TYPE_CATEGORY = 2;
	
	private void toggle(int type, boolean isSelected, int value) {
		switch (type) {
		case TOGGLE_TYPE_SEASON:
			if (isSelected) {
				mSeason += value;
			} else {
				mSeason -= value;
			}
			break;
		case TOGGLE_TYPE_TIMEZONE:
			if (isSelected) {
				mTimezone += value;
			} else {
				mTimezone -= value;
			}
			break;
		default:
			if (isSelected) {
				mCategory += value;
			} else {
				mCategory -= value;
			}
			break;
		}
	}
}
